package com.deductive.web.disposition.algorithm;

import cn.hutool.core.lang.tree.Tree;
import cn.hutool.core.lang.tree.TreeNodeConfig;
import cn.hutool.core.lang.tree.TreeUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.deductive.common.core.controller.BaseController;
import com.deductive.common.core.domain.AjaxResult;
import com.deductive.common.core.page.TableDataInfo;
import com.deductive.domain.algorithm.AlgorithmBattleInjuryRescue;
import com.deductive.service.algorithm.AlgorithmBattleInjuryRescueService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("algorithmBattleInjuryRescue")
@Api(tags = "战伤救治方案管理")
public class AlgorithmBattleInjuryRescueController extends BaseController {

    @Resource
    private AlgorithmBattleInjuryRescueService sbirService;

    @GetMapping("/pageQuery")
    @ApiOperation("分页查询")
    public TableDataInfo selectAll(AlgorithmBattleInjuryRescue sbir) {
        startPage();
        List<AlgorithmBattleInjuryRescue> list = this.sbirService.list(new QueryWrapper<>(sbir));
        return getDataTable(list);
    }

    @GetMapping("/list")
    @ApiOperation("战伤救治方案目录查询")
    public AjaxResult list(AlgorithmBattleInjuryRescue sbir) {
        LambdaQueryWrapper<AlgorithmBattleInjuryRescue> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(sbir.getName()!=null,AlgorithmBattleInjuryRescue::getName,sbir.getName());
        List<AlgorithmBattleInjuryRescue> list = sbirService.list(queryWrapper);
        TreeNodeConfig treeNodeConfig = new TreeNodeConfig();
        treeNodeConfig.setIdKey("id");
        treeNodeConfig.setNameKey("name");
        treeNodeConfig.setParentIdKey("parentId");
        treeNodeConfig.setWeightKey("orderNum");

        List<Tree<Object>> build = TreeUtil.build(list, 0L, treeNodeConfig, (map, tree) -> {
            tree.setId(map.getId());
            tree.setName(map.getName());
            tree.setParentId(map.getParentId());
            tree.putExtra("particulars", map.getParticulars());
            tree.putExtra("bigType", map.getBigType());
            tree.putExtra("smallType", map.getSmallType());
            tree.putExtra("orderNum", map.getOrderNum());
        });

        if(build == null || build.isEmpty()){
            // 当没有可用的父子关系时，按平铺返回节点，避免返回空
            List<Tree<Object>> flat = new ArrayList<>();
            for (AlgorithmBattleInjuryRescue map : list) {
                Tree<Object> tree = new Tree<>();
                tree.setId(map.getId());
                tree.setName(map.getName());
                tree.setParentId(map.getParentId());
                tree.putExtra("particulars", map.getParticulars());
                tree.putExtra("bigType", map.getBigType());
                tree.putExtra("smallType", map.getSmallType());
                tree.putExtra("orderNum", map.getOrderNum());
                flat.add(tree);
            }
            return AjaxResult.success(flat);
        }
        return AjaxResult.success(build);
    }

    @GetMapping("{id}")
    @ApiOperation("根据id查询")
    public AjaxResult selectOne(@PathVariable Integer id) {
        return success(this.sbirService.getById(id));
    }

    @PostMapping
    @ApiOperation("新增数据")
    public AjaxResult insert(@RequestBody AlgorithmBattleInjuryRescue sbir) {
        return success(this.sbirService.save(sbir));
    }

    @PutMapping
    @ApiOperation("修改数据")
    public AjaxResult update(@RequestBody AlgorithmBattleInjuryRescue sbir) {
        return success(this.sbirService.updateById(sbir));
    }

    @DeleteMapping
    @ApiOperation("删除数据")
    public AjaxResult delete(@RequestParam("idList") List<Long> idList) {
        return success(this.sbirService.removeByIds(idList));
    }
}
